package com.polaris.live.utils.sensor.impl

import androidx.appcompat.app.AppCompatActivity
import com.just.agentweb.AgentWeb
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.utils.sensor.SensorModuleHelper
import com.polaris.live.utils.sensor.base.DialogModule
import com.polaris.live.utils.sensor.base.FunctionModule
import com.polaris.live.utils.sensor.base.IModule
import ren.yale.android.cachewebviewlib.WebViewCacheInterceptorInst

/**
 * Web模块帮助类
 *
 * @author Created by lucas on 2023/10/25 16:03
 * @since 1.0.0
 */
class WebModuleHelper(
    context: AppCompatActivity,
    private val mAgentWeb: AgentWeb
) : SensorModuleHelper(context) {

    override fun showSetting(): Boolean {
        val module = object : DialogModule(mContext) {

            override val title: String = "Web Tools"

            override fun createModules(): Array<IModule> = arrayOf(
                FunctionModule(mContext, "Clear web cache", {
                    mAgentWeb.clearWebCache()
                    WebViewCacheInterceptorInst.getInstance().clearCache()

                    ToastGravity.showGravity("Web cache cleared successfully")
                })
            )
        }
        module.setOnDismissListener { mIsShowed = false }
        module.runModule()

        return module.isShowDialog()
    }

    companion object {

        fun attach(context: AppCompatActivity, agentWeb: AgentWeb): WebModuleHelper {
            return WebModuleHelper(context, agentWeb)
        }
    }
}